package com.lzt.yijian.mvp.view.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lzt.yijian.R;
import com.lzt.yijian.adapter.MovieAdapter;
import com.lzt.yijian.bean.Movie;
import com.lzt.yijian.bean.Subjects;
import com.lzt.yijian.mvp.base.BaseFragment;
import com.lzt.yijian.mvp.presenter.HomeFragmentPresenter;
import com.lzt.yijian.mvp.presenter.HomeFragmentPresenterImpl;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class HomeFragment extends BaseFragment implements HomeFragmentView {

    @BindView(R.id.home_recycler_view)
    RecyclerView mHomeRecyclerView;


    private List<Subjects> mSubjects = new ArrayList<>();
    private Unbinder mBind;
    HomeFragmentPresenter mPresenter;
    private MovieAdapter mMovieAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_home, container, false);
        mBind = ButterKnife.bind(this, layout);
        mPresenter = new HomeFragmentPresenterImpl(mContext, this);
        return layout;
    }

    @Override
    public void onResume() {
        super.onResume();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        mHomeRecyclerView.setLayoutManager(linearLayoutManager);
        mMovieAdapter = new MovieAdapter(mContext, mSubjects);
        mHomeRecyclerView.setAdapter(mMovieAdapter);
        mPresenter.onGetData();
    }

    @Override
    public void onGetDataDone(Movie movie) {
        mSubjects.addAll(movie.getSubjects());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mBind.unbind();
    }


}
